package me.leiusual.sharing.ui.store

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import kotlinx.android.synthetic.main.fragment_store_detail.*
import me.leiusual.sharing.R
import me.leiusual.sharing.api.ApiSharing
import me.leiusual.sharing.ui.base.AbstractWebViewFragment
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-25
 * description:
 */
class StoreDetailFragment : AbstractWebViewFragment() {

  companion object {
    private const val DESCRIPTION = "description"

    fun newInstance(description: String): StoreDetailFragment {
      return StoreDetailFragment().apply {
        val args = Bundle()
        args.putString(DESCRIPTION, description)
        arguments = args
      }
    }
  }

  @Inject
  lateinit var factory: ViewModelProvider.Factory

  private val viewModel: StoreDetailViewModel by viewModels { factory }

  override val layoutWebView: LinearLayout
    get() = layout_web_view

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_store_detail, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val description = arguments?.getString(DESCRIPTION) ?: ""
    webView.loadDataWithBaseURL(ApiSharing.BASE_URL, description, "text/html", "utf-8", null)

    observeData()
  }

  private fun observeData() {
    viewModel
  }
}